package library.and.xps.com.step.view.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import library.and.xps.com.step.EventBusCommond;
import library.and.xps.com.step.R;
import library.and.xps.com.step.adapter.RankingTabViewAdapter;
import library.and.xps.com.step.view.base.BaseFragment;

public class RankingFragment extends BaseFragment {
    TabLayout tabLayout;
    ArrayList<String> tab_names;
    ArrayList<Fragment> frags = new ArrayList<>();
    ViewPager vp_order;
    RankingTabViewAdapter rankingTabViewAdapter;

    @Override
    protected int getRes() {
        return R.layout.fragment_ranking;
    }

    @Override
    protected void init(View view) {
        tabLayout = r2v(view, R.id.tabLayout);
        r2v(view, R.id.iv_menubt).setOnClickListener(this);
        vp_order = r2v(view, R.id.vp_order);

        frags.add(new PersonRankingFragment());
        frags.add(new TeamRankingFragment());
        tab_names = new ArrayList<>();
        tab_names.add("个人排名");
        tab_names.add("团队排名");
        tabLayout.addTab(tabLayout.newTab().setText(tab_names.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(tab_names.get(1)));
        rankingTabViewAdapter = new RankingTabViewAdapter(getActivity().getSupportFragmentManager(), frags, tab_names);
        vp_order.setAdapter(rankingTabViewAdapter);
        tabLayout.setupWithViewPager(vp_order);
    }

    @Override
    protected void setData(View view) {

    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commitAllowingStateLoss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_menubt:
                EventBus.getDefault().post(EventBusCommond.OPEN_DRAWERMENU);
                break;
        }
    }
}
